Coverage Report

Created: 2024-12-19 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\gen\file\content.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use itertools::Itertools;
30
use std::fmt::Display;
31
32
pub struct Content<'a> {
33
    header: Option<&'a str>,
34
    body: String,
35
    footer: Option<&'a str>,
36
}
37
38
impl<'a> Content<'a> {
39
105
    pub fn from_iter<D: Display>(mut iter: impl Iterator<Item = D>) -> Self {
40
105
        Self {
41
105
            header: None,
42
105
            body: iter.join("\n"),
43
105
            footer: None,
44
105
        }
45
105
    }
46
47
106
    pub fn try_from_iter<D: Display, E>(iter: impl Iterator<Item = Result<D, E>>) -> Result<Self, E> {
48
106
        let data = iter.collect::<Result<Vec<D>, E>>()
?0
;
49
106
        Ok(Self {
50
106
            header: None,
51
106
            body: data.iter().join("\n"),
52
106
            footer: None,
53
106
        })
54
106
    }
55
56
0
    pub fn from_string(value: String) -> Self {
57
0
        Self {
58
0
            header: None,
59
0
            body: value,
60
0
            footer: None,
61
0
        }
62
0
    }
63
64
0
    pub fn header(&mut self, header: &'a str) -> &mut Self {
65
0
        self.header = Some(header);
66
0
        self
67
0
    }
68
69
0
    pub fn footer(&mut self, footer: &'a str) -> &mut Self {
70
0
        self.footer = Some(footer);
71
0
        self
72
0
    }
73
74
211
    pub fn to_string(&self) -> Option<String> {
75
211
        if self.body.is_empty() {
  Branch (75:12): [True: 122, False: 89]
  Branch (75:12): [Folded - Ignored]
76
122
            return None;
77
89
        }
78
89
        Some(match (self.header, self.footer) {
79
0
            (Some(header), None) => format!("{header}{}", self.body),
80
0
            (Some(header), Some(footer)) => format!("{header}{}{footer}", self.body),
81
0
            (None, Some(footer)) => format!("{}{footer}", self.body),
82
89
            (None, None) => self.body.to_string(),
83
        })
84
211
    }
85
}